// views/mine/myhomepage/index.js
var util = require('../../../utils/util');
var BASE_URL = getApp().globalData.BASE_URL


Page({

  /**
   * 页面的初始数据
   */
  data: {
    showdata: true,
    nodata: false,
    cate: '',
    active: 0,
    userinfo: '',
    articleList: '',
    answerlist: '',
    current_page: '', //上拉加载更多当前页
    last_page: '', //上拉加载更多最后页
    showflag: false, //控制上拉加载更多的出现和隐藏,
    showfinish: false, //控制没有数据的出现和隐藏
    zhuangtai: '关注'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    if (util.iflogin()) {
      if (options.flag) {
        this.setData({
          zhuangtai: '已关注',
        })
      }
      this.setData({
        id: options.id,
      })
      var _this = this;
      wx.request({
        url: `${BASE_URL}/user/dongtai/type//`,
        method: "POST",
        data: {
          id: _this.data.id
        },
        header: {
          'Content-Type': 'application/json' //GET方式是这个
        },
        success(res) {
          console.log(res)
          res.data.list.data.forEach(item => {
            item.content = '',
              item.other_info = '',
              item.time = util.formatTimeTwo(item.time, 'Y-M-D');
            if (item.cover.length < '60' && item.cover.length !== 0) {
              item.cover = `${BASE_URL}/` + item.cover
            }

          })
          if (res.data.user.avatar.length < '55') {
            res.data.user.avatar = `${BASE_URL}` + res.data.user.avatar
          }
          console.log(res.data.user.avatar.length)
          _this.setData({
            userinfo: res.data.user,
            showdata: false,
            articleList: res.data.list.data,
            current_page: res.data.list.current_page,
            last_page: res.data.list.last_page,
          })
          if (res.data.list.data[0] == undefined) {
            _this.setData({
              nodata: true
            })
          }
        }
      })
    }
  },

  onChange: function (e) {
    console.log(e)
    if (e.detail.name == 0) {
      e.detail.name = ''
    }
    this.setData({
      cate: e.detail.name,
      showdata: true,
      articleList: '',
      nodata: false
    })
    console.log(e.detail.name)
    var _this = this;
    wx.request({
      url: `${BASE_URL}/user/dongtai/type/${e.detail.name}/`,
      method: "POST",
      data: {
        id: _this.data.id
      },
      header: {
        'Content-Type': 'application/json' //GET方式是这个
      },
      success(res) {
        res.data.list.data.forEach(item => {
          item.content = '',
            item.other_info = '',
            item.time = util.formatTimeTwo(item.time, 'Y-M-D');
            if (item.cover.length < '60' && item.cover.length !== 0) {
              item.cover = `${BASE_URL}/` + item.cover
            }
        })
        console.log(res, )
        if (res.data.list.data[0] !== undefined) {
          if (res.data.list.data[0].cate == 'ask') {
            _this.setData({
              answerlist: res.data.list.data,
              current_page: res.data.list.current_page,
              last_page: res.data.list.last_page,
              showdata: false,
            })
          } else if (res.data.list.data[0].cate == 'article') {
            _this.setData({
              articleList: res.data.list.data,
              current_page: res.data.list.current_page,
              last_page: res.data.list.last_page,
              showdata: false,

            })
          } else {
            _this.setData({
              articleList: res.data.list.data,
              current_page: res.data.list.current_page,
              last_page: res.data.list.last_page,
              showdata: false,


            })
          }

        } else {
          _this.setData({
            showdata: false,
            nodata: true
          })
        }
      }
    })
  },

  onReachBottom: util.throttle(function () {
      var _this = this
      if (_this.data.current_page !== _this.data.last_page) {
        _this.setData({
          current_page: _this.data.current_page + 1,
          showflag: true,
          showfinish: false,
        })
      } else {
        _this.setData({
          showflag: false,
          showfinish: true,
        })
      }
      if (_this.data.showflag) {
        setTimeout(function () {
          wx.request({
            url: `${BASE_URL}/user/dongtai/type/${_this.data.cate}/?page=${_this.data.current_page}`,
            data: {
              id: _this.data.id
            },
            header: {
              'Content-Type': 'application/json', //GET方式是这个
            },
            method: "POST",
            success(res) {
              res.data.list.data.forEach(item => {
                item.content = '',
                  item.other_info = '',
                  item.time = util.formatTimeTwo(item.time, 'Y-M-D');
                if (item.cover.length < '60' && item.cover.length !== 0) {
                  item.cover = `${BASE_URL}/` + item.cover
                }
              })
              console.log(res)
              _this.setData({
                articleList: _this.data.articleList.concat(res.data.list.data),
                showflag: false,
              })
            }
          })
        }, 500)
      }
    },
    2000),

  //跳转文章详情页
  articleDetail: function (e) {
    var id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: `/views/txtdetail/index?id=${id}`
    })
  },
})